﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using Cashier_Model;
using Cashier_Service;
using System.Windows.Forms;
using Cashier.usercontrols;
using Cashier.utils;
using System.Collections;
using Cashier_Interface;

namespace Cashier.supplier
{
    public partial class SupplierAddForm : FormExt
    {
        private IReloadData iReloadData;
        private Object iParam;
        private long supplierid;
        private Supplier supplier;
        private SupplierService supplierService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainform"></param>
        /// <param name="iReloadData"></param>
        /// <param name="iParam"></param>
        /// <param name="supplierid"></param>
        public SupplierAddForm(MainForm mainform, IReloadData iReloadData, Object iParam, long supplierid)
        {
            InitializeComponent();
            this.iReloadData = iReloadData;
            this.iParam = iParam;
            this.supplierid = supplierid;
            this.supplierService = new SupplierService();

            if (supplierid > 0)
                button_save.Tag = "supplier_edit";
            else
                button_save.Tag = "supplier_add";
            Button[] buttons = { button_save };
            RightsforControls.RightsforButton(mainform.Admin.RolePurviewtable, buttons);
            this.InitialForm();
        }

        /// <summary>
        /// 初始化表单数据
        /// </summary>
        private void InitialForm()
        {
            try
            {
                if (supplierid > 0)
                {
                    supplier = supplierService.GetSupplier(supplierid, null);
                    if (supplier != null)
                    {
                        FormUtil.InitialFormData(supplier, this, 1);
                        if (supplier.Sex.Equals("男"))
                            radioButton1_sex.Checked = true;
                        else
                            radioButton2_sex.Checked = true;
                    }
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        /// <summary>
        /// 保存供应商信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_save_Click(object sender, EventArgs e)
        {
            try
            {
                int depth = 0;
                ArrayList list = new ArrayList();
                FormUtil.GetValidateControlsNotPassed(this, list, depth);
                if (list.Count > 0)
                    return;

                if (supplier == null)
                {
                    supplier = new Supplier();
                    supplier.Createdate = DateTime.Now;
                }
                FormUtil.GetFormData(supplier, this, 1);
                String sex = String.Empty;
                if (radioButton1_sex.Checked)
                    sex = radioButton1_sex.Text;
                else if (radioButton2_sex.Checked)
                    sex = radioButton2_sex.Text;
                supplier.Sex = sex;
                
                bool issuccess = false;
                if (supplier.Id == 0)
                {
                    long id = supplierService.InsertSupplier(supplier);
                    if (id > 0)
                        issuccess = true;
                }
                else
                {
                    int flag = supplierService.UpdateSupplier(supplier);
                    if (flag > 0)
                        issuccess = true;
                }
                if (issuccess)
                {
                    DialogResult result = MessageBox.Show("保存成功，是否添加新记录！", "提示", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        supplier = null;
                        FormUtil.ClearFormData(this, 1);
                    }
                    else
                    {
                        this.Close();
                        this.iReloadData.ReloadDataGridViewData(iParam);
                    }
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }
        
        /// <summary>
        /// 取消关闭窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// ESC键退出窗体
        /// </summary>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessDialogKey(Keys keyData)
        {
            bool b = base.ProcessDialogKey(keyData);
            if (keyData == Keys.Escape)
            {
                b = false;
                this.Close();
            }
            return b;
        }
    }
}
